package leetcode_2501_2600;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class LeeCode_2551 {
    public static void main(String[] args) {
        System.out.println(putMarbles(new int[]{1, 3, 5, 1}, 2));
    }
    private static long putMarbles(int[] weights, int k) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < weights.length - 1; i++) {
            list.add(weights[i] + weights[i + 1]);
        }
        list.sort(Comparator.comparingInt(o -> o));
        long min = 0;
        for (int i = 0; i < k - 1; i++) {
            min += list.get(i);
        }
        long max = 0;
        for (int i = list.size() - k + 1; i < list.size(); i++) {
            max += list.get(i);
        }
        return max - min;
    }
}
